package cn.gson.springboot.model.service.presaleservice;

import cn.gson.springboot.model.dao.presaledao.BusinessInfoDao;
import cn.gson.springboot.model.dao.presaledao.DetailsDao;
import cn.gson.springboot.model.dao.presaledao.OfferDao;
import cn.gson.springboot.model.dao.presaledao.OfferDetailsDao;
import cn.gson.springboot.model.mapper.presalemapper.PsBusinessDetailsTwoMapper;
import cn.gson.springboot.model.pjos.*;
import cn.gson.springboot.model.vo.privilegevo.ApprovalPosition;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 商机详情2业务类
 */
@Service
@Transactional
public class PsBusinessDatailsTwoService {
    @Autowired
    PsBusinessDetailsTwoMapper businessDetailsTwoMapper;

    @Autowired
    BusinessInfoDao businessInfoDao;//商机dao

    @Autowired
    DetailsDao detailsDao;//注入商机产品dao

    @Autowired
    OfferDao offerDao;//注入报价dao

    @Autowired
    OfferDetailsDao offerDetailsDao;//注入报价详情dao
    /*编辑产品*/
    public  void saveDetails(String details){
        JSONObject obj = (JSONObject) JSONObject.parse(details);
        Integer businesId=obj.getInteger("businesId");//取出商机id
        //先查询该商机下的所有产品
        List<Details> list = detailsList(businesId);
        //然后执行删除
        detailsDao.deleteAll(list);
        //取出数组里的数据
        List detailsList=obj.getObject("productList",List.class);
        //转化成JSONArray
        JSONArray json = JSONArray.parseArray(String.valueOf(detailsList));
        JSONObject object=null;
        Details details1;
        BusinessInfo businessInfo = new BusinessInfo();
        for (int k=0;k<json.size();k++){
            object = (JSONObject) json.get(k);//将每一条数据转换成JSONObject类型
            Integer detNum=object.getInteger("detNum");
            BigDecimal detTotal = object.getBigDecimal("detTotal");
            String detDiscount = object.getString("detDiscount");
            Integer prsId=object.getInteger("prsId");
            BigDecimal detPrice = object.getBigDecimal("detPrice");
            BigDecimal detDiscountedPrice = object.getBigDecimal("detDiscountedPrice");
            BigDecimal detFactoryrice = object.getBigDecimal("detFactoryrice");
            businessInfo.setBusinesId(businesId);
            details1 = new Details(0,detNum,detTotal,detDiscount,businessInfo,prsId,detPrice,detDiscountedPrice,detFactoryrice);
            detailsDao.save(details1);
        }
        BusinessInfo businessInfo1 = businessInfoDao.findById(businesId).get();
        businessInfo1.setUpdateTime(new Timestamp(new Date().getTime()));
        businessInfoDao.save(businessInfo1);
    }

    /*查询商机下产品*/
    public List<Details> detailsList(Integer businesId){
        return businessDetailsTwoMapper.selectDetails(businesId);
    }

    /*查询报价单的最大数字*/
    public String selectMaxOfferNumber(){
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMdd");
        Date date = new Date();//获取当前时间
        String code = businessDetailsTwoMapper.selectMaxOfferNnumber();
        //如果查询出来的数字为空，则为当天第一个
        if(null == code || "".equals(code)){
            code = "O"+sdf.format(date)+"-"+"1";
        }else{
            //数字不为空则加一，
            int zhi = Integer.parseInt(code)+1;
            code = "O"+sdf.format(date)+"-"+zhi+"";
        }
        return code;
    }
    /*查询商机下的报价单*/
    public List<Offer> selectBusinessOffer(Integer businesId){
        return businessDetailsTwoMapper.selectBusinessOffer(businesId);
    }
    public int saveOffer(String offerData){
        JSONObject obj = (JSONObject) JSONObject.parse(offerData);
        Offer offer1 = obj.getObject("offer",Offer.class);
        offer1.setOfferStatus(2);
        offer1.setOfferTime(new Timestamp(new Date().getTime()));
        offerDao.save(offer1);
        List offerDetailsList=obj.getObject("offerDetails",List.class);
        JSONArray detailsJson = JSONArray.parseArray(String.valueOf(offerDetailsList));
        JSONObject object=null;
        Zproductside productside = new Zproductside();
        OfferDetails details;
        for(int i=0;i<detailsJson.size();i++){
            object = (JSONObject) detailsJson.get(i);
            String prsDiscount = object.getString("detDiscount");
            Integer prsNum = object.getInteger("detNum");
            BigDecimal prsCount = object.getBigDecimal("detTotal");
            Integer prsId = object.getInteger("prsId");
            BigDecimal detPrice = object.getBigDecimal("detPrice");
            BigDecimal detDiscountedPrice = object.getBigDecimal("detDiscountedPrice");
            productside.setPsId(prsId);
            details = new OfferDetails(0,prsDiscount,prsNum,prsCount,offer1,productside,detPrice,detDiscountedPrice);
            offerDetailsDao.save(details);
        }
        Integer count=businessDetailsTwoMapper.countOffer(offer1.getBusinessInfoByBusinesId().getBusinesId());
        System.out.println("count:"+count);
        if(count ==1){
            System.out.println("推推");
            SpeedInfo speedInfo = businessDetailsTwoMapper.selectOne("报价设计");
            BusinessInfo businessInfo = businessInfoDao.findById(offer1.getBusinessInfoByBusinesId().getBusinesId()).get();
            businessInfo.setSpeedInfoBySpeedId(speedInfo);
            businessInfoDao.save(businessInfo);
            return 1;
        }
        BusinessInfo businessInfo1 = businessInfoDao.findById(offer1.getBusinessInfoByBusinesId().getBusinesId()).get();
        businessInfo1.setUpdateTime(new Timestamp(new Date().getTime()));
        businessInfoDao.save(businessInfo1);
        return 0;
    }
    public Offer offerOne(Integer offerId){
        return businessDetailsTwoMapper.offerOne(offerId);
    }

    public List<OfferDetails> offerTwo(Integer offerId){
        return businessDetailsTwoMapper.offerTwo(offerId);
    }

    /*查询所有的商机进度*/
    public List<SpeedInfo> allSpeed(){
        return businessDetailsTwoMapper.allSpeed();
    }

    /*根据进度内容查询出进度id*/
    public SpeedInfo selectOne(String speedContext){
        return businessDetailsTwoMapper.selectOne(speedContext);
    }
    /* 统计商机下的跟进记录*/
    public Integer countFollowUp(Integer businesId){
        return businessDetailsTwoMapper.countFollowUp(businesId);
    }

    /*统计商机下的解决方案*/
    public Integer countProgramme(Integer businesId){
        return businessDetailsTwoMapper.countProgramme(businesId);
    }

    /*统计商机下的报价单*/
    public Integer countOffer(Integer businesId){
        return businessDetailsTwoMapper.countOffer(businesId);
    }

    /**拥有审批权限的人*/
    public List<ApprovalPosition> findApprovaler(String meName){
        return  businessDetailsTwoMapper.findApprovaler(meName);
    }

}
